package com.fenghongzhang.day014;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

import java.util.Timer;


public class MyService extends Service {

    private static final String TAG = "MyService";
    public MyService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: ");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");



        //前台服务(通知)
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("标题");

        //普通通知.
//        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        manager.notify(1,builder.build());

        //去哪个包下,拿哪个布局.
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);
        remoteViews.setTextViewText(R.id.notify_tv,"哈哈");
        remoteViews.setImageViewResource(R.id.notify_img,R.mipmap.ic_launcher);
        remoteViews.setTextViewText(R.id.notify_btn,"播放");

        builder.setCustomContentView(remoteViews);

        //发送前台服务
        startForeground(2,builder.build());
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //停止前台服务
        stopForeground(true);
        Log.i(TAG, "onDestroy: ");
    }


    public void show(){
        Log.i(TAG, "show: ");
    }

    public void show2(){
        //前台服务(通知)
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("标题");

        //普通通知.
//        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        manager.notify(1,builder.build());

        //去哪个包下,拿哪个布局.
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);
        remoteViews.setTextViewText(R.id.notify_tv,"呵呵呵");
        remoteViews.setImageViewResource(R.id.notify_img,R.mipmap.ic_launcher);
        remoteViews.setTextViewText(R.id.notify_btn,"暂停");

        builder.setCustomContentView(remoteViews);

        //发送前台服务
        startForeground(2,builder.build());
        Log.i(TAG, "show2: ");
    }

    //代理人
    class MyBinder extends Binder{
        public void callShow(){
            show();
        }

        public void callShow2(){
            show2();
        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind: ");
        return new MyBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "onUnbind: ");
        return super.onUnbind(intent);
    }
}
